package com.example.star.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.star.R;
import com.example.star.entity.Reply;

import java.util.List;

public class ReplyAdapter extends BaseAdapter {

    private Context context;
    private List<Reply> replyList;

    public ReplyAdapter(Context context, List<Reply> replyList) {
        this.context = context;
        this.replyList = replyList;
    }

    @Override
    public int getCount() {
        return replyList.size();
    }

    @Override
    public Object getItem(int position) {
        return replyList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_reply, parent, false);
        } else {
            view = convertView;
        }

        Reply reply = replyList.get(position);
        ImageView avatarImageView = view.findViewById(R.id.avatar_image_view);
        TextView adminUsernameTextView = view.findViewById(R.id.admin_username_text_view);
        TextView messageTextView = view.findViewById(R.id.admin_message_text_view);


        // 显示消息内容
        messageTextView.setText(reply.getMessage());
        adminUsernameTextView.setText(reply.getAdminName());

        // 显示用户头像
        if (reply.getUser() != null && reply.getUser().getAvatar() != null) {
            Glide.with(context)
                    .load(reply.getUser().getAvatar())
                    .into(avatarImageView);
        }

        return view;
    }
}